//审核
var recruitId = null;
var recruit_state = null;
$(function () {
    var recruit_datagrid = $("#recruit_datagrid");
    var recruit_dialog = $("#recruit_dialog");
    var recruit_form = $('#recruit_form');
    //recruit_datagrid 数据表初始化
    recruit_datagrid.datagrid({
        url: '/recruit/list',
        pagePosition: 'bottom',
        striped: "true",
        pagination: "true",
        singleSelect: "true",
        fit: "true",
        fitColumns: "true",
        toolbar: '#recruit_toolbar',
        columns: [[
            {field: 'x', checkbox: true},
            {field: 'id', title: '编号', width: 100, sortable: true, hidden: true},

            {field: 'inputTime', title: '制定日期', width: 100, align: 'center'},

            {field: 'doTime', title: '执行日期', width: 100, align: 'center'},

            {
                field: "plantype", title: '计划类别', width: 100, align: 'center', formatter: function (value) {
                return value ? value.name : '';
            }
            },

            {field: "planName", title: '计划名称', width: 100, align: 'center'},

            {
                field: 'inputUser', title: '制定人', width: 100, align: 'center', formatter: function (value) {
                return value ? value.realName : '';
            }
            },

            {
                field: 'approver', title: '审批人', width: 100, align: 'center', formatter: function (value) {
                return value ? value.realName : '';
            }
            },

            {
                field: 'state', title: '审批状态', width: 100, align: 'center',
                formatter: function (value) {
                    switch (value) {
                        case 0:
                            return "<span style='color: #ecff36'>待审批</span>";
                        case 1:

                            return "<span style='color: #16ff34'>已审批</span>";
                        case 2:
                            return "<span style='color: red'>审批被拒</span>";
                        default:
                    }

                }
            }
        ]]
    })
    //recruit_datagrid 数据表初始化-------end

    //明细对话框 初始化
    recruit_dialog.dialog({
        width: 1200,
        height: 500,
        closed: true,
        buttons: "#dialog_buttons",
    });
    //明细对话框 初始化 ----end

    //审核对话框初始化
    $("#approve_dialog").dialog({
        width: 300,
        height: 150,
        closed: true,
        buttons: '#app'
    })
    //end

    //弹框的弹框 初始化
    $("#recruit_add_dialog").dialog({
        width: 700,
        height: 280,
        closed: true,
        buttons: '#dialog_buttons_detail'
    })
    //end


    var cmdObj = {
        reload: function () {
            recruit_datagrid.datagrid("reload")
        },

        /*  add: function () {
         //清除form 表单 缓存数据
         recruit_form.form('clear');
         recruit_dialog.dialog("setTitle", "招聘计划增加");
         recruit_dialog.dialog("open");
         },*/

        //高级查询
        query: function () {

            //时间
            var beginDate = $("#beginDate").datebox("getValue");
            var endDate = $("#endDate").datebox("getValue");
            var plantypeId = $("#plantypeId").combobox("getValue");
            $("#recruit_datagrid").datagrid("load",{
                beginDate:beginDate,
                endDate:endDate,
                plantypeId:plantypeId
            })

        },


        //删除
        remove: function () {
            //判断是否选中
            var row = $("#recruit_datagrid").datagrid("getSelected");
            if (row == null) {
                $.messager.alert('温馨提示', "请选中需要删除的数据");
                return;
            }

            if (row.state == 1) {
                $.messager.alert('温馨提示', "禁止删除");
                return;
            }

            $.get("/recruit/removeRecruit?id=" + row.id, function (data) {

                if (data.success) {
                    $("#recruit_datagrid").datagrid("reload")
                    // $("#approve_dialog").dialog("close");
                } else {
                    $.messager.alert('温馨提示', data.msg);
                }
            })
        },


        //计划保存
        save: function () {

            // alert(recruitId)

            var url = "/recruit/save";
            if (recruitId) {
                url = "/recruit/update";
            }
            recruit_form.form('submit', {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        recruit_dialog.dialog("close");
                        $("#recruit_datagrid").datagrid("load");

                    } else {
                        $("#recruit_datagrid").datagrid("load");
                        $.messager.alert('温馨提示', data.msg);
                    }
                }
            });

        },

        //审核方案2
        changeState_2: function () {
            var row =  $("#recruit_datagrid").datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            recruitId = row.id;
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.open({
                    content: "确认同意"+"[" + row.planName  + "]"+"计划么?"
                    , btn: ['同意', '不同意', '取消']
                    , yes: function (index, layero) {
                        //录取了
                        $.ajax({
                            url: '/recruit/changeState?ret=1&recruitId=' + recruitId,
                            // data: {id: row.id},
                            dataType: 'json',
                            type: 'get',
                            success: function (data) {
                                if (data.success) {
                                    layer.open({
                                        content: "已将"+"[" + row.planName  + "]"+"的计划详细信息添加到人才需求中,请在其中录入应聘者信息录入",
                                        btn: ['确认'],
                                        yes: function (index, layero) {
                                            $("#recruit_datagrid").datagrid("reload");
                                            layer.close(index);
                                        }
                                    });
                                } else {
                                    layer.open({
                                        content: data.msg,
                                        btn: ['确认'],
                                        yes: function (index, layero) {
                                            $("#recruit_datagrid").datagrid("reload");
                                            layer.close(index);
                                        }
                                    });
                                }
                            }
                        });

                    }
                    , btn2: function (index, layero) {
                        //被干掉了
                        $.ajax({
                            url: "/recruit/changeState?ret=2&recruitId=" + recruitId,
                           // data: {id: row.id},
                            //  dataType: 'json',
                            type: 'get',
                            success: function (data) {
                                if (data.success) {
                                    layer.open({
                                        content: "操作成功",
                                        btn: ['确认'],
                                        yes: function (index, layero) {
                                            $("#recruit_datagrid").datagrid("reload");
                                            layer.close(index);
                                        }
                                    });
                                } else {
                                    layer.open({
                                        content: data.msg,
                                        btn: ['确认'],
                                        yes: function (index, layero) {
                                            $("#recruit_datagrid").datagrid("reload");
                                            layer.close(index);
                                        }
                                    });
                                }
                            }
                        });
                    },
                    btn3: function (index, layero) {
                        layer.close(index);
                    }
                });
            });
        },



/*
        //审核
        changeState:function(){
            //判断是否选中
            var row = recruit_datagrid.datagrid("getSelected");
          //  alert(row.id)
            if (row == null) {
                $.messager.alert('温馨提示', "请选中需要审核的数据");
                return;
            }
            recruitId = row.id;

            if (row.state != 0) {
                $.messager.alert('温馨提示', "已禁止审核");
                return;
            }

            //打开审核弹框
            $("#approve_dialog").dialog("open")
            $("#approve_dialog").dialog("setTitle", "审核")
        },


        //同意
        agree: function () {
            $.get("/recruit/changeState?ret=1&recruitId=" + recruitId, function (data) {

                if (data.success) {
                    recruit_datagrid.datagrid("reload")
                    $("#approve_dialog").dialog("close");
                } else {
                    $.messager.alert('温馨提示', data.msg);
                }
            })
        },

        //不同意
        disagree: function () {
            $.get("/recruit/changeState?ret=2&recruitId=" + recruitId, function (data) {

                if (data.success) {
                    recruit_datagrid.datagrid("reload")
                    $("#approve_dialog").dialog("close");
                } else {
                    $.messager.alert('温馨提示', data.msg);
                }
            })
        },*/


        cancel: function () {

            recruit_dialog.dialog("close")
        },

        //增加明细
        add_detail: function () {
            if (recruit_state != 0 && recruit_state != null) {
                $.messager.alert('温馨提示', "不能修改了,已经审核了!");
                return;
            }
            if (recruitId == null) {
                $.messager.alert('温馨提示', "请先保存一次,再增加!");
                return;
            }
            //  alert(recruitId)


            $("#recruit_add_dialog").dialog("open");
            $("#recruit_add_dialog").dialog("setTitle", "招聘计划详单条目增加");
            $("#recruit_add_form").form("clear");
        },


        //保存单个明细
        save_detail: function () {
            if (recruit_state != 0) {
                $.messager.alert('温馨提示', "不能再保存,已经审核了!");
                return;
            }

            //  alert(recruitId)
            var url = "/recruitDetail/save";
            $('#recruitId').textbox().textbox('setValue', recruitId);
            $("#recruit_add_form").form('submit', {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        recruit_datagrid.datagrid("reload");
                        $("#recruit_add_dialog").dialog("close");
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                }
            });
            $("#recruit_add_form").form("load");

            $("#recruitPlanDetail").datagrid("load");
            $("#recruit_add_dialog").dialog("close");

            $("#recruit_dialog").dialog("refresh");

        },
        //单个名细的保存

        //明细删除
        remove_detail: function () {
            if (recruit_state != 0 && recruit_state != null) {
                $.messager.alert('温馨提示', "已审核禁止删除");
                return;
            }
            //判断是否选中
            var row = $("#recruitPlanDetail").datagrid("getSelected");
            if (row == null) {
                $.messager.alert('温馨提示', "请选中需要删除的数据");
                return;
            }
            $.get("/recruitDetail/delete?id=" + row.id, function (data) {

                if (data.success) {
                    $("#recruitPlanDetail").datagrid("reload")
                    $.messager.alert('温馨提示', "删除成功");
                } else {
                    $.messager.alert('温馨提示', data.msg);
                }
            })
        }

    }


    //代码优化
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });


})

//添加计划
function add() {
    //清除form 表单 缓存数据

    /*
     var data = '{"total":0,"rows":[]}';
     data = Jsonpa(data);//转换成json对象，必须的！！！不然有异常！！！
     $("#recruitPlanDetail").datagrid('loadData', data);*/




    $("#recruit_form").form('clear');

    $("#recruit_dialog").dialog("setTitle", "招聘计划增加");

    $("#recruit_dialog").dialog("open");

    var url = null;
    saveorUpdate(url)
}


//编辑查看明细
function edit() {

    //判断是否选中
    var row = $("#recruit_datagrid").datagrid("getSelected")
    if (row == null) {
        $.messager.alert('温馨提示', "请选中需要编辑的数据");
        return;
    }
    recruitId = row.id;
    recruit_state = row.state;
    //清空 旧数据
    $("#recruit_form").form('clear');

    //同名匹配  总表的回显问题
    if (row.inputUser) {
        row["inputUser.realName"] = row.inputUser.realName;
    }
    if (row.plantype) {
        row["plantype.id"] = row.plantype.id;
    }

    var id = row.id;
    var url = "/recruitDetail/queryByRecruit?recruitId=" + id;
    saveorUpdate(url)
    //把数据加载到 对话框的表单中
    $("#recruit_form").form('load', row);
    $("#recruit_dialog").dialog("setTitle", "招聘计划编辑");
    $("#recruit_dialog").dialog("open");
}


function saveorUpdate(url) {

    //招聘计划明细表
    $("#recruitPlanDetail").datagrid({
        title: "招聘计划明细表",
        striped: "true",
        pagination: "true",
        singleSelect: "true",
        height: 500,
        width: 1200,
        url: url,
        rownumbers: true,
        fitColumns: true,
        columns: [[
            {field: 'x', checkbox: true},
            {
                field: 'id', title: '编号', width: 100, sortable: true, hidden: true,
            },
            {field: 'job', width: 100, align: 'center', title: '招聘岗位', editor: 'text'},

            {
                field: 'recruit',
                title: '制定人id',
                width: 100,
                align: 'center',
                hidden: true,
                formatter: function (value) {
                    return value ? value.id : '';
                }
            },

            {
                field: 'recruitType', width: 100, align: 'center', title: '招聘方式',
                formatter: function (value, row) {
                    if (value) {
                        return value.name;
                    }
                    return '';
                }
            },
            {
                field: 'engage', width: 100, align: 'center', title: '聘用形式',

                formatter: function (value, row) {
                    if (value) {
                        return value.name;
                    }
                    return '';
                }
            },

            {field: 'recruitNumber', width: 100, align: 'center', title: '人数', editor: 'text'},

            {
                field: 'education', width: 100, align: 'center', title: '学历要求',

                formatter: function (value, row) {
                    if (value) {
                        return value.name;
                    }
                    return '';
                }
            },
            {field: 'skillRequired', width: 100, align: 'center', title: '技能要求', editor: 'text'},
            {
                field: 'recruitSalary', width: 100, align: 'center', title: '薪资情况',
                formatter: function (value, row) {
                    if (value) {
                        return value.name;
                    }
                    return '';
                }
            },
            {field: 'remark', width: 100, align: 'center', title: '备注', editor: 'text'}
        ]],


    })

}




